package com.xidata.dict.demo.controller;

import com.xidata.cloud.common.ServiceResponse;
import com.xidata.dict.client.embedded.DataStructure;
import com.xidata.dict.client.embedded.aop.CodeTranslate;
import com.xidata.dict.demo.db.MenuItem;
import com.xidata.dict.service.CodeDictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/menu")
@Api(value = "微信接口")
@Slf4j
public class MenuController {

    @Resource
    CodeDictService codeDictService;


    @ApiOperation(value = "搜索")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @CodeTranslate(clazz = ServiceResponse.class, codeType = "menu", data = "response", structure = DataStructure.Array)
    public ServiceResponse<List<MenuItem>> list() {
        List<MenuItem> list = new ArrayList<>();
        list.add(new MenuItem("main", "主菜单"));
        list.add(new MenuItem("shop", "店铺"));
        list.add(new MenuItem("product", "商品"));

        return ServiceResponse.success(list);
    }

    @GetMapping("/detail/{id}")
    @ApiOperation("明细")
    @CodeTranslate(clazz = ServiceResponse.class, codeType = "menu", data = "response", structure = DataStructure.Object)
    public ServiceResponse<MenuItem> detail(@PathVariable("id") String id) {
        return ServiceResponse.success(new MenuItem("main", "主菜单"));
    }

    @GetMapping("/tree")
    @ApiOperation("树形")
    @CodeTranslate(clazz = ServiceResponse.class, codeType = "menu", data = "response", structure = DataStructure.Tree, childrenProperty = "subMenus")
    public ServiceResponse<MenuItem> tree() {
        MenuItem menuItem = new MenuItem("main", "主菜单");
        menuItem.add(new MenuItem("shop", "店铺"));
        menuItem.add(new MenuItem("product", "商品"));

        return ServiceResponse.success(menuItem);
    }

    @GetMapping("/clear")
    @ApiOperation("清除字典")
    public ServiceResponse<Void> clear() {
        codeDictService.removeAll();
        return ServiceResponse.success(null);
    }

    @GetMapping("/init")
    @ApiOperation("初始化")
    public ServiceResponse<Void> init() {
        codeDictService.add("menu", "main", "en", "home");
        return ServiceResponse.success(null);
    }


}
